﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ASmile.Win
{
    public static class EasyCode
    {
        public static bool ToEasyCode(this string src, out string pyCode, out string wbCode)
        {
            var r = DictHelper.GetEasyCode(src);
            if (r == null)
            {
                pyCode = null;
                wbCode = null;
                return false;
            }
            pyCode = r.PY;
            wbCode = r.WB;
            return true;
        }

        public static string ToPYCode(this string src)
        {
            return DictHelper.GetPYCode(src);
        }

        public static string ToWBCode(this string src)
        {
            src.ToEasyCode(out _, out string wbCode);
            return wbCode;
        }

        public static void SetEasyCode(this TextBox src, TextBox pyBox, TextBox wbBox = null)
        {
            src.Leave -= Src_Leave;
            src.Leave += Src_Leave;
            void Src_Leave(object sender, EventArgs e)
            {
                if (string.IsNullOrWhiteSpace(src.Text)) return;

                if (src.Text.ToEasyCode(out string pyCode, out string wbCode))
                {
                    if (pyBox != null)
                    {
                        pyBox.Text = pyCode;
                    }
                    if (wbBox != null)
                    {
                        wbBox.Text = wbCode;
                    }
                }
            }
        }
    }
}